﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace TCL.EP.SPCommon
{
    public class SPListItemHelper
    {
        public static SPWeb GetAdminWeb(string siteURL)
        {
            SPWeb adminWeb = null;
            SPSecurity.RunWithElevatedPrivileges(delegate
            {

                using (SPSite site = new SPSite(siteURL))
                {
                    using (adminWeb = site.OpenWeb())
                    {

                    }
                }
            });
            return adminWeb;
        }

        public static string GetItemString(object o)
        {
            if (o == null)
                return "";
            return o.ToString();
        }

        public static TaxonomyFieldValue GetItemTaxonomyField(object o)
        {
            if (o == null)
            {
                return null;
            }

            TaxonomyFieldValue taxonomy = o as TaxonomyFieldValue;
            return taxonomy;
        }

        public static SPFieldUrlValue GetItemSPFieldUrlValue(object o)
        {
            if (o == null)
            {
                return null;
            }
            SPFieldUrlValue url = new SPFieldUrlValue(o.ToString());
            return url;
        }

        public static string GetItemSPfieldUrlString(object o)
        {
            SPFieldUrlValue url = GetItemSPFieldUrlValue(o);
            if (url != null)
            {
                return url.Url;
            }
            return "";
        }

        public static bool? GetItemBool(object o)
        {
            if (o == null)
            {
                return null;
            }
            bool result;
            string str = o.ToString();
            bool check = bool.TryParse(str, out  result);
            if (check)
                return result;
            return null;

        }

        public static int GetItemInt(object o)
        {
            if (o == null)
                return -1;
            string str = o.ToString();
            int value;

            bool check = Int32.TryParse(str, out value);
            if (check)
                return value;
            return -1;
        }

        public static SPFieldUserValueCollection ChangeSPFieldUserValueTOUserCollection(object o, SPWeb web)
        {
            SPFieldUserValue userValue = new SPFieldUserValue(web, GetItemString(o));
            SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
            if (userValue != null && userValue.User != null)
            {
                userCollection.Add(userValue);
            }
            return userCollection;
        }

        public static DateTime? GetItemDateTime(object o)
        {
            string strObject = GetItemString(o);

            DateTime date;

            bool isSuccessfull = DateTime.TryParse(strObject, out date);
            if (isSuccessfull)
            {
                return date;
            }
            else
            {
                return null;
            }
        }

    }
}
